<?php
class model_lhbase_authrule extends gdl_components_basemodel 
{

    public function primarykey() {
        return 'id';
    }

    public function tableName() {
        return 'lhc_auth_rule';
    }
	
	 public function getList($field = 'id,name', $where = 'isdelete=0 AND status=1')
    {
		return $this->select($where, $field)->items;
    }
	
	public function getMenu($userid,$appId=1,$identity='gl'){
		if(empty($userid)) return [];
		$authuseraccess = new model_lhbase_authuseraccess();
		$authgrouprule = new model_lhbase_authgrouprule();
		$authgroupaccess = new model_lhbase_authgroupaccess();

		

		$tableName =  $this->tableName();
		$leftJoin = array(
			$authuseraccess->tableName().' as a' => 'a.ruleId='.$tableName.'.id'
		);
		if($identity!='zg'){

			$autRuleId = $authuseraccess->select(['userId'=>$userid],'ruleId')->items;
			$ruleId = empty($autRuleId) ? [0] :array_column($autRuleId,'ruleId');
			$menuRuleId = self::superiorRule($ruleId);

			$condition = '('.$tableName.'.status=1 and a.userId='.$userid.' and a.appId='.$appId.') or '.$tableName.'.id in('.implode(',',$menuRuleId ).') and '.$tableName.".component<>''"; //
			
		$access = [];
		$accessNode = $this->select($condition, $tableName.'.id,'.$tableName.'.name,'.$tableName.'.name,'.$tableName.'.pid,'.$tableName.'.title,'.$tableName.'.accessCode,'.$tableName.'.component,'.$tableName.'.condition','order by '.$tableName.'.accessCode,'.$tableName.'.pid asc', '', $leftJoin)->items;
			
		} else {
			$condition = "component<>''";
			$access = [];
			$accessNode = $this->select($condition, $tableName.'.id,'.$tableName.'.name,'.$tableName.'.pid,'.$tableName.'.title,'.$tableName.'.accessCode,'.$tableName.'.component,'.$tableName.'.condition','order by '.$tableName.'.accessCode,'.$tableName.'.pid asc')->items;
			
		}
		
		foreach($accessNode as $v){
			$access[$v['id']] = $v;
		}
		return $access;
	}

	public function allMenu(){
		$menu = [];
		$accessNode = $this->select(' 1 ', 'id,accessCode')->items;
		foreach($accessNode as $v){
			$menu[$v['id']] = $v['accessCode'];
		}
		return $menu;
	}

	public function buttons($menu=[],$menuId=[]){
		if(empty($menu) || empty($menuId) ) return [];
		$buttons = [];
		$allMenu = $this->allMenu();
		
		foreach($menu as $v){
			if(!empty($v['condition'])){
				$condition = json_decode($v['condition'],true);

				if(!empty($condition['elAuth'])){
					foreach($condition['elAuth'] as $vk){
						if(in_array($vk,$menuId)){
							$buttons[] = ['attribution'=>$v['id'],'name'=>$allMenu[$vk],'status'=>1];
						}/* else {
							$buttons[] = ['attribution'=>$v['id'],'name'=>$allMenu[$vk],'status'=>0];
						}*/
					}
				}
			}
		}
		return $buttons;
	}
	
	//查找上级
	public function superiorRule($ruleId,$menuId=[-1]){
		if(empty($ruleId)) return $menuId;
		$autRuleId = $this->select('id in('.implode(',',$ruleId).')','pid')->items;

		$differences = empty($autRuleId) ? [-1] : array_column($autRuleId,'pid');
		$differencesRuleId = array_diff($differences,$menuId);
		if(!empty($differencesRuleId)){
			$menuId = array_merge($differencesRuleId,$menuId);
			$menuId = self::superiorRule($differencesRuleId,$menuId);
		}
		return $menuId;
	}
	
	//查找下级节点
	public function dnRule($ruleId,$menuId=[]){
		if(empty($ruleId)) return $menuId;
		if(empty($menuId)) $menuId=$ruleId;
		$autRuleId = $this->select('pid in('.implode(',',$ruleId).')','id')->items;

		$differences = empty($autRuleId) ? [-1] : array_column($autRuleId,'id');
		$differencesRuleId = array_diff($differences,$menuId);

		if(!empty($differencesRuleId)){
			$menuId = array_merge($differencesRuleId,$menuId);

			$menuId = self::superiorRule($differencesRuleId,$menuId);
		}

		return $menuId;
	}

	
	
}
